using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Data.SqlClient;
using System.Threading;
using SiteManager;


/// <summary>
/// Summary description for HCMailService
/// </summary>
public class HCMailService 
{    
        //private System.Diagnostics.EventLog eventLog1;
        ////private EventLog eventLog1;
        //private System.ComponentModel.Container components = null;
        ////creating instance of Manager class       

                   
        //public s()
    public HCMailService()
        {
            //InitializeComponent();

            //if (!System.Diagnostics.EventLog.SourceExists("HCMail"))
            //{
            //    System.Diagnostics.EventLog.CreateEventSource("HCMail", "HCMailLog");
            //}
            //eventLog1.Source = "HCMail";
            //eventLog1.Log = "HCMailLog";

         
            //this.ServiceName = "";
            //((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();

        }

        //public bool SendMail()
    public string SendMail()
        {
            StringBuilder sb = new StringBuilder();
            bool bln = false;
            try
            {
                Manager _mngr = new Manager(true,"ConnectionString");//instance of manager class

                //string time = System.Configuration.ConfigurationManager.AppSettings["time"].ToString();

                //eventLog1.WriteEntry(DateTime.Now.ToLongTimeString());
                //eventLog1.WriteEntry(time);

                string strMailTo = string.Empty;
                string strMailFrom = string.Empty;
                string strMailCC = string.Empty;                

                strMailFrom = System.Configuration.ConfigurationManager.AppSettings["fromId"].ToString();
                //strMailFrom = "ranjeet.singh@sampatti.com";


                //if (System.Configuration.ConfigurationManager.AppSettings["mode"].ToString().Equals("debug"))
                //{ strMailTo = System.Configuration.ConfigurationManager.AppSettings["debugId"].ToString(); }
                //else
                //{
                //    strMailTo = System.Configuration.ConfigurationManager.AppSettings["liveId"].ToString();
                //    strMailCC = System.Configuration.ConfigurationManager.AppSettings["CC"].ToString();
                //}

                strMailTo = System.Configuration.ConfigurationManager.AppSettings["liveId"].ToString();
                strMailCC = System.Configuration.ConfigurationManager.AppSettings["CC"].ToString();

                //strMailCC = "hr@sampatti.com";
                //"7:28:58 PM"
                //"6:11:15 PM"
                //6:18 PM
                //6:19 PM

                //if (DateTime.Now.ToLongTimeString() == time)
                //{
                    //eventLog1.WriteEntry("In Time");

                    object obj = _mngr.ExecuteScalarQuery("select count(id) from tbl_Maildetails where convert(varchar,today,101)=convert(varchar,getdate(),101)");

                    //eventLog1.WriteEntry(Convert.ToInt32(obj.ToString()).ToString());

                    if (Convert.ToInt32(obj.ToString()) == 0)
                    {
                        //eventLog1.WriteEntry(DateTime.Now.ToString("MM/dd/yyyy"));                        

                        SqlParameter[] _param = new SqlParameter[2];
                        _param[0] = new SqlParameter("@EntryDt", SqlDbType.Char, 10);
                        _param[0].Value = DateTime.Now.AddDays(-1).ToString("MM/dd/yyyy");
                        _param[1] = new SqlParameter("@FBflag", SqlDbType.Char, 1);
                        _param[1].Value = "B";

                        //Added at 13/03/2008

                        SqlParameter[] _paramDen = new SqlParameter[2];
                        _paramDen[0] = new SqlParameter("@EntryDt", SqlDbType.Char, 10);
                        _paramDen[0].Value = DateTime.Now.AddDays(-1).ToString("MM/dd/yyyy");
                        _paramDen[1] = new SqlParameter("@FBflag", SqlDbType.Char, 1);
                        _paramDen[1].Value = "B";

                        //End
                        DataSet ds = _mngr.ExecuteQuery("sp_SiteAnalytics", _param, "tbl");
                        DataSet dsDent = _mngr.ExecuteQuery("sp_DentistSiteAnalytics", _paramDen, "tbl");
                        
                        //  eventLog1.WriteEntry(ds.Tables.Count.ToString());

                        sb.Append("Hi John, <br><br>");
                        
                        // Doctor Statistics Starts
                        if (ds.Tables.Count == 1)
                        {
                            sb.Append("<b>Doctor's Daily Records on "+ DateTime.Now.AddDays(-1).ToString("MMM dd yyyy")+" </b><br><br><br>");
                            for (int i = 0; i < 5; i++)
                            {
                                sb.Append(ds.Tables[0].Rows[i][0].ToString() + " " + ds.Tables[0].Rows[i][1].ToString() + "<br>");
                            }
                        }
                        else if (ds.Tables.Count == 2)
                        {
                            sb.Append("<b>Weekly Records:</b><br><br>");
                            for (int i = 0; i < 5; i++)
                            {
                                sb.Append(ds.Tables[0].Rows[i][0].ToString() + " " + ds.Tables[0].Rows[i][1].ToString() + "<br>");
                            }
                            sb.Append("<b><br>Daily Records:</b><br><br>");
                            for (int i = 0; i < 5; i++)
                            {
                                sb.Append(ds.Tables[1].Rows[i][0].ToString() + " " + ds.Tables[1].Rows[i][1].ToString() + "<br>");
                            }
                        }
                        else if (ds.Tables.Count == 3)
                        {
                            sb.Append("<b>Monthly Records:</b><br><br>");
                            for (int i = 0; i < 5; i++)
                            {
                                sb.Append(ds.Tables[0].Rows[i][0].ToString() + " " + ds.Tables[0].Rows[i][1].ToString() + "<br>");
                            }
                            sb.Append("<b><br>Weekly Records:</b><br><br>");
                            for (int i = 0; i < 5; i++)
                            {
                                sb.Append(ds.Tables[1].Rows[i][0].ToString() + " " + ds.Tables[1].Rows[i][1].ToString() + "<br>");
                            }
                            sb.Append("<b><br>Daily Records:</b><br><br>");
                            for (int i = 0; i < 5; i++)
                            {
                                sb.Append(ds.Tables[2].Rows[i][0].ToString() + " " + ds.Tables[2].Rows[i][1].ToString() + "<br>");
                            }
                        }
                        // Doctor Statistics Ends

                        // Dentist Statistics Starts
                        if (dsDent.Tables.Count == 1)
                        {
                            sb.Append("<br/><b>Dentist's Daily Records on " + DateTime.Now.AddDays(-1).ToString("MMM dd yyyy") + " </b><br><br><br>");
                            for (int i = 0; i < 4; i++)
                            {
                                sb.Append(dsDent.Tables[0].Rows[i][0].ToString() + " " + dsDent.Tables[0].Rows[i][1].ToString() + "<br>");
                            }
                        }
                        else if (dsDent.Tables.Count == 2)
                        {
                            sb.Append("<b>Weekly Records:</b><br><br>");
                            for (int i = 0; i < 4; i++)
                            {
                                sb.Append(dsDent.Tables[0].Rows[i][0].ToString() + " " + dsDent.Tables[0].Rows[i][1].ToString() + "<br>");
                            }
                            sb.Append("<b><br>Daily Records:</b><br><br>");
                            for (int i = 0; i < 4; i++)
                            {
                                sb.Append(dsDent.Tables[1].Rows[i][0].ToString() + " " + dsDent.Tables[1].Rows[i][1].ToString() + "<br>");
                            }
                        }
                        else if (dsDent.Tables.Count == 3)
                        {
                            sb.Append("<b>Monthly Records:</b><br><br>");
                            for (int i = 0; i < 4; i++)
                            {
                                sb.Append(dsDent.Tables[0].Rows[i][0].ToString() + " " + dsDent.Tables[0].Rows[i][1].ToString() + "<br>");
                            }
                            sb.Append("<b><br>Weekly Records:</b><br><br>");
                            for (int i = 0; i < 4; i++)
                            {
                                sb.Append(dsDent.Tables[1].Rows[i][0].ToString() + " " + dsDent.Tables[1].Rows[i][1].ToString() + "<br>");
                            }
                            sb.Append("<b><br>Daily Records:</b><br><br>");
                            for (int i = 0; i < 4; i++)
                            {
                                sb.Append(dsDent.Tables[2].Rows[i][0].ToString() + " " + dsDent.Tables[2].Rows[i][1].ToString() + "<br>");
                            }
                        }
                        // Dentist Statistics Ends

                        sb.Append("<br>");
                        sb.Append("<br>");
                        sb.Append("Regards,<br>");
                        sb.Append("FindaDoc<br>");
                        
                        if (!bln)
                        {
                            if (!strMailCC.Equals(string.Empty))
                            {
                                bln = true;
                                _mngr.sendMail(sb.ToString(), strMailTo, "Health Care", "Doctor's Information", strMailFrom, strMailCC, "");                                
                            }
                            else
                            {
                                bln = true;
                                string []strmailID = strMailTo.Split(new char[]{';'});
                                foreach (string s in strmailID)
                                {

                                   _mngr.sendMail(sb.ToString(), s, "Health Care", "Doctor's Information", strMailFrom, "");    
                                }
                                
                            }
                        }
                       _mngr.ExecuteUpdate("update tbl_maildetails set today=getdate();");
                    }
                //}
            }
            catch (Exception)
            {                
            //    eventLog1.WriteEntry(this.ServiceName + " Error: " + ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
           // return bln;            //13/03/2008
            return sb.ToString();
        }
}
